package com.zhkj.txg.module.home.adapter

import android.graphics.Paint
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.module.LoadMoreModule
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.zhkj.lib.utils.AccountManager
import com.zhkj.lib.utils.SpanUtil
import com.zhkj.txg.R
import com.zhkj.txg.module.home.entity.SellEntity
import com.zhkj.txg.module.product.ui.ProductDetailActivity
import com.zhkj.txg.utils.ImageUtils

class TopProductAdapter :
    BaseQuickAdapter<SellEntity, BaseViewHolder>(R.layout.item_home_sell), LoadMoreModule {

    init {
        setOnItemClickListener { _, _, position ->
            val entity = data[position]
            ProductDetailActivity.normalProduct(context, entity.id)
        }
    }

    override fun convert(helper: BaseViewHolder, item: SellEntity) {
        with(item) {
            val progress = salesSum.toFloat() / (salesSum + storeCount) * 100
            val imageView = helper.setText(R.id.tvSellTitle, goodName)
                .setText(R.id.tvSellPrice, SpanUtil.setMoneySymbol(price,true))
                .setText(R.id.tvSellCount, String.format(context.getString(R.string.sell_count), salesSum))
                .setText(R.id.tvSellProgress, String.format(context.getString(R.string.sell_percent), progress.toInt()))
                .getView<ImageView>(R.id.ivSellThumb)
            ImageUtils.loadImage(context, imageView, image)
            helper.getView<ProgressBar>(R.id.pbSellProgress).progress = progress.toInt()
            // 设置是显示市场价还是显示分享赚多少钱
            with(helper.getView<TextView>(R.id.tvSellSpread)) {
                if (AccountManager.isMember() && shareEarn.isNotEmpty() && shareEarn != "0") {
                    setTextColor(ContextCompat.getColor(context, R.color.colorMain))
                    paint.flags = Paint.ANTI_ALIAS_FLAG
                    text = SpanUtil.setPrefixMoneyStr(price = shareEarn)
                } else {
                    setTextColor(ContextCompat.getColor(context, R.color.colorLightText))
                    paint.flags = Paint.STRIKE_THRU_TEXT_FLAG
                    paint.isAntiAlias = true
                    text = SpanUtil.setMoneySymbol(marketPrice)
                }
            }
        }
    }
}